Commands.php

<?php

namespace Tlf\Scrawl;

trait Commands {

    public function run_init($cli, $args){
        if (
            $this->prompt("Create sample files for code scrawl in '".$cli->pwd."'? (y/n)", "y")
            !="y"
        ) {
            return false;
        } 

        $files = \Tlf\Scrawl\Utility::allFilesFromDir(dirname(__DIR__).'/test/input/Project/', '', []);
        foreach ($files as $f){
            if (substr($f->relPath,0,6)=='/docs/')continue;
            $file = $cli->pwd.'/'.$f->relPath;
            $dir = dirname($file);
            if (!is_dir($dir))mkdir($dir);
            if (!is_file($file)){
                file_put_contents($file, $f->content());
            }
        }

    }

    public function generate_docs($cli, $args){

        echo json_encode($args, JSON_PRETTY_PRINT);
        // print_r($args);
        if (
            $this->prompt("Generate code-scrawl docs in '".$this->dir."' with above configs? (y/n)", "y")
            !="y"
        ) {
            return false;
        } 

        $this->add_standard_extensions();

        // add extensions as configured
        foreach ($this->configs['scrawl.ext'] as $class=>$type){
            $this->addExtension(new $class($this));
        }

        $dryRun = $args['dryRun'] ?? false;

        $this->extCall('default', 'onBuildStart');
    // 1. Find files to document
        $codeFiles = $this->getFilesToDocument();
        $this->extCall('default', 'onFileListPrepared', $codeFiles);
    // 2. Call InputExtensions for each file
        foreach ($codeFiles as $cf){
            $this->extCall('default', 'onSourceFileFound', $cf);
        }
    // 3. 
        $this->extCall('default', 'onSourceFilesDone');


    // 4. Find all template files
        $templateFiles = $this->getTemplateFiles();

        $this->extCall('default', 'onTemplatesListPrepared', $templateFiles);

    // 5. Call OutputExtensions for each template file
        foreach ($templateFiles as $tf){
            $this->extCall('default', 'onTemplateFileFound', $tf);
        }

        $this->extCall('default', 'onPreWriteFiles');

    // 6. Write outputs to disk (or call WriterExtensions?)
        $outputFiles = $this->getOutputs('file');

        //@export_start(Technical.deleteExistingDocs.sanityCheck)
        if (!$dryRun&&$this->configs['deleteExistingDocs'][0]===true){
            $root = $this->dir;
            $root = realpath($root);
            $docsDir = $this->dir.'/'.$this->configs['dir.docs'][0];
            $docsDir = realpath($docsDir);

            // To avoid removing things like /home/user/ and /var/www/ or shorter paths
            // My assumption for shortest path is: /home/user/asubdir/projectdirs
            // Projects COULD be at /home/user/projectdir, but mine likely never will be... & I think that seems silly.
            // I expect Windows to have even longer base paths?
            $rootSlashArray = explode('/', str_replace('\\', '/', $root));
            $docSlashArray = explode('/', str_replace('\\','/', $docsDir));
            if (strlen($docsDir)>strlen($root)
                && $root!=''
                && count($rootSlashArray)>3
                && count($docSlashArray) > count($rootSlashArray)
                && strpos($docsDir, $root)===0
            ) {
                \Tlf\Scrawl\Utility::DANGEROUS_removeNonEmptyDirectory($docsDir);
            }
        }
        //@export_end(Technical.deleteExistingDocs.sanityCheck)
 
        // return;
        foreach ($outputFiles as $relPath => $content){
            $of = new File($this->dir, $this->configs['dir.docs'][0].'/'.$relPath, $content);
            $this->extCall('default', 'onWillWriteFile', $of);
            $this->writeFile($of, $dryRun);
        }

    }

    public function prompt($message, $default){
        if ($this->configs['noprompt'][0] === true) {
            return $default;
        }

        return readline($message);
    }
}